package com.nfu.zlsp.dev.controller;

import com.nfu.zlsp.dev.common.constants.TheConstant;
import com.nfu.zlsp.dev.common.utils.PageUtils;
import com.nfu.zlsp.dev.common.utils.R;
import com.nfu.zlsp.dev.entity.TOrderEntity;
import com.nfu.zlsp.dev.entity.UserEntity;
import com.nfu.zlsp.dev.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author txh
 * @email hunterapeman@gmail.com
 * @date 2023-03-12 01:54:16
 */
@RestController
@RequestMapping("dev/torder")
public class TOrderController {
    @Autowired
    private TOrderService tOrderService;




    @RequestMapping("/updateOrderStateByid/{id}")
    public R getGoods(@PathVariable("id") String id){
        R r = tOrderService.updateOrderStateById(id);
        return r;
    }



    /**
     * 托运方完成一键拼单
     * @param params
     * @param session
     * @return
     */
    @Transactional
    @RequestMapping("/payTOrder")
    public R payTOrder(@RequestParam Map<String, Object> params, HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO);
        if (user == null) {
            return R.ok().put("data", "用户登录信息过期,请重新登录");
        }
        //获取当前托运方个人信息id
        params.put("c_userid",user.getId());

        R r = tOrderService.savePayList(params);
        return r;
    }


    /**
     * 承运方 ‘订单管理数据获取’
     */
    @RequestMapping("/myOrderManagementTransport")
    public R queryOrderManagementTransport(@RequestParam Map<String, Object> params, HttpSession session){

        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO_TRANSPORT);
        if (user == null) {
            return R.ok().put("data", "用户登录信息过期,请重新登录");
        }
        //获取当前托运方个人信息id
        params.put("t_user",user.getId());
        PageUtils page = tOrderService.queryOrderManagementConsignPage(params);
        page.setTotalCount(page.getList().size());
        int totalPage = page.getTotalCount()% page.getPageSize()==0?page.getTotalCount()/ page.getPageSize():page.getTotalCount()/ page.getPageSize()+1;
        page.setTotalPage(totalPage);
        return R.ok().put("page", page);
    }

    /**
     * 托运方 ‘我拼的单数据获取’
     */
    @RequestMapping("/myOrderConsign")
    public R queryMyOrderConsign(@RequestParam Map<String, Object> params, HttpSession session){

        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO);
        if (user == null) {
            return R.ok().put("data", "用户登录信息过期,请重新登录");
        }
        //获取当前托运方个人信息id
        params.put("c_userid",user.getId());
        PageUtils page = tOrderService.queryMyOrderConsignPage(params);
        page.setTotalCount(page.getList().size());
        int totalPage = page.getTotalCount()% page.getPageSize()==0?page.getTotalCount()/ page.getPageSize():page.getTotalCount()/ page.getPageSize()+1;
        page.setTotalPage(totalPage);
        return R.ok().put("page", page);
    }







    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("dev:torder:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tOrderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("dev:torder:info")
    public R info(@PathVariable("id") String id){
		TOrderEntity tOrder = tOrderService.getById(id);
        return R.ok().put("tOrder", tOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TOrderEntity tOrder){
		tOrderService.save(tOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TOrderEntity tOrder){
		tOrderService.updateById(tOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		tOrderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
